package com.spit.fammo.ui.frg

import android.os.Bundle
import android.view.View
import android.widget.ExpandableListView
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.NetworkUtils
import com.kingja.loadsir.core.LoadService
import com.spit.amoeba.base.BaseFragment
import com.spit.fammo.R
import com.spit.fammo.adapter.bindadapter.UploadAdapter
import com.spit.fammo.bean.DataBean
import com.spit.fammo.databinding.FUploadBinding
import com.spit.fammo.ext.loadServiceInit
import com.spit.fammo.ext.showEmpty
import com.spit.fammo.ext.showLoading
import com.spit.fammo.ext.showMessage
import com.spit.fammo.viewmodel.RfidModel
import com.spit.fammo.viewmodel.UploadModel
import me.hgj.jetpackmvvm.base.appContext

/**
 * User: Nike
 *  2025/1/10 14:09
 *      上传
 */
class UploadFrg: BaseFragment<UploadModel, FUploadBinding>(), ExpandableListView.OnGroupClickListener, ExpandableListView.OnChildClickListener{

    val listDrawBean: ArrayList<DataBean> by lazy { ArrayList() }

    val adapter: UploadAdapter by lazy { UploadAdapter(requireContext(), listDrawBean) }

    lateinit var loadsir: LoadService<Any>

    val rfidModel: RfidModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        loadsir = loadServiceInit(mDatabind.expandableListView) {
            loadsir.showLoading()
            mViewModel.onRequest()
        }
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()
//        mDatabind.expandableListView.setOnGroupClickListener(this)
        mDatabind.expandableListView.setOnChildClickListener(this)

        mDatabind.expandableListView.setAdapter(adapter)

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                setToolBarTitle(getString(R.string.upload))
                mViewModel.onRequest()
            }
        })

        /*mDatabind.expandableListView.setOnGroupExpandListener { groupPosition ->
            for (i in 0 until adapter.groupCount) {
                if (i != groupPosition) {
                    mDatabind.expandableListView.collapseGroup(i)
                }
            }
        }*/
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(this, {
            listDrawBean.clear()
            if (it.size != 0){
                loadsir.showSuccess()
                listDrawBean.addAll(it)
                adapter.notifyDataSetChanged()
                mDatabind.expandableListView.expandGroup(0)
            }else{
                adapter.notifyDataSetChanged()
                isAllEmpty()
            }
        })
        mViewModel.uploadStockTakeSuccess.observe(this, {
//            val parentBean = listDrawBean[it.first]
//            val child = parentBean.expandableChild
//            child.removeAt(it.second)
//            if (child.size == 0){
//                listDrawBean.removeAt(it.first)
//            }
//            adapter.notifyDataSetChanged()
//            isAllEmpty()
            mViewModel.onRequest()
            rfidModel.mainDrawerUploadNum.value = 0
        })
    }

    private fun isAllEmpty(){
        if (listDrawBean.size == 0){
            loadsir.showEmpty()
        }
        rfidModel.mainDrawerUploadNum.value = 0
    }

    inner class ProxyClick() {

        fun onConfirm(){
            if (listDrawBean.size == 0)return
            if (!NetworkUtils.isConnected()){
                showMessage(appContext.getString(R.string.no_internet))
                return
            }
            mViewModel.allUpload(listDrawBean)
        }

    }

    override fun onGroupClick(p0: ExpandableListView?, p1: View?, p2: Int, p3: Long): Boolean {
        mDatabind.expandableListView.expandGroup(p2)
        return true
    }

    override fun onChildClick(
        p0: ExpandableListView?,
        p1: View?,
        groupPosition: Int,
        childPosition: Int,
        p4: Long
    ): Boolean {
        if (!NetworkUtils.isConnected()){
            showMessage(appContext.getString(R.string.no_internet))
        }else{
            val parentBean = listDrawBean[groupPosition]
            val bean = parentBean.expandableChild.get(childPosition)
            mViewModel.uploadStockTake(bean, groupPosition, childPosition)
        }
        return true
    }

}